<template>
  <el-row>
    <el-col :span="12">
      <div class="form-main" ref="formBox">
        <el-card>
          <div slot="header" class="text-primary">
            <span class="title"> 密码设置 </span>
          </div>
          <el-form
            ref="form"
            :rules="rules"
            :model="form"
            status-icon
            label-width="80px"
          >
            <el-form-item label="用户名" prop="username">
              <el-input disabled v-model="form.username"></el-input>
            </el-form-item>

            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password" type="password"></el-input>
            </el-form-item>

            <el-form-item label="确认密码" prop="pwdConfirm">
              <el-input v-model="form.pwdConfirm" type="password"></el-input>
            </el-form-item>
            <div>
              <el-button
                style="margin-left: 80px"
                @click="submitForm('form')"
                type="primary"
                >提交</el-button
              >
            </div>
          </el-form></el-card
        >
      </div>
    </el-col>
  </el-row>
</template>
<script>
import { getInfo, updatePwd } from '@/api/accountApi'
export default {
  data() {
    let validatePwd2 = (rule, value, callback) => {
      console.log(rule)
      if (value == '') {
        callback(new Error('请输入确认密码'))
      } else if (value.length < 6 || value.length > 30) {
        callback(new Error('密码长度为（6~30）'))
      } else if (value != this.form.password) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    return {
      windowHeight: 0,
      rules: {
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 30, message: '注意长度6~30', trigger: 'blur' },
        ],
        pwdConfirm: [
          {
            required: true,
            validator: validatePwd2,
            trigger: 'blur',
          },
        ],
      },
      form: {
        id: 0,
        username: '',
        password: '',
        pwdConfirm: '',
      },
    }
  },
  created() {
    this.$nextTick(() => {
      this.getInfo()
    })
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updatePwd(this.form).then((resp) => {
            let response = resp.data
            if (response.code == 1) {
              this.$message({
                message: response.message,
                type: 'success',
              })
              this.getInfo()
            } else {
              this.$message({
                message: response.message,
                type: 'error',
              })
            }
          })
        } else {
          this.$message({
            message: '表单验证失败',
            type: 'error',
          })
        }
      })
    },
    getInfo() {
      let account = JSON.parse(localStorage.getItem('account'))
      let account_id = account.id
     // console.log(this.$route.params)
      if (this.$route.query.id) account_id = this.$route.query.id
      getInfo({ id: account_id }).then((resp) => {
        let response = resp.data

        if (response.code == 1) {
          this.form = response.data
        } else {
          this.$message('获取信息失败')
        }
        console.log(resp)
      })

      console.log('account_id:' + account_id)
    },
  },
}
import '@/assets/css/variables.css'
</script>
<style scoped>
.el-card {
  background: var(--bg-color);
  color: var(--text-color);
  border-color: var(--border-color);
}
::v-deep .el-card__header {
  border-bottom: 1px solid var(--border-color);
}
::v-deep .el-input__inner {
  background-color: var(--bg-color);
  color: var(--text-color);
  border: 1px solid var(--border-color);
}
::v-deep .el-input.is-disabled .el-input__inner {
  background-color: var(--bg-color);
  border: 1px solid var(--border-color);
}
</style>
